package com.leyou.item.service;

import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 岩冰
 * @version 1.0
 * @date:2018/9/29
 */
@Service
public class SpecificationService {
    @Autowired
    private SpecGroupMapper specGroupMapper;
    public List<SpecGroup> querySpecGroups(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        return specGroupMapper.select(specGroup);
    }

    public void addSpecGroup(SpecGroup specGroup) {
        specGroupMapper.insert(specGroup);
    }

    public void updateSpecGroup(SpecGroup specGroup) {
        specGroupMapper.updateByPrimaryKeySelective(specGroup);
    }

    public void deleteSpecGroup(Long id) {

    }

    @Autowired
    private SpecParamMapper specParamMapper;
    public List<SpecParam> querySpecParams(Long gid,Long cid,Boolean searching, Boolean generic) {

        return specParamMapper.select(new SpecParam().
                setGroupId(gid).setCid(cid).setSearching(searching).
                setGeneric(generic));
    }

    public void deleteSpecParam(Long id) {
        specParamMapper.deleteByPrimaryKey(id);
    }

    public void addSpecParam(SpecParam specParam) {
        specParamMapper.insert(specParam);
    }

    public void updateSpecParam(SpecParam specParam) {
        specParamMapper.updateByPrimaryKey(specParam);
    }

    public List<SpecGroup> querySpecsByCid(Long cid) {
        List<SpecGroup> specGroups = this.querySpecGroups(cid);
        specGroups.forEach(specGroup -> specGroup.setSpecParams(this.querySpecParams(specGroup.getId(), null, null, null)));
        return specGroups;
    }
}
